package com.xxzb.pack0820;

import java.io.*;
import java.util.Scanner;

//带缓冲流的字节操作
public class DemoD {
    //带缓冲的读取操作
    public static void a() throws Exception {
        File file = new File("F:\\yongpangaofeng\\a.txt");
        InputStream in = new FileInputStream(file);
        //创建缓冲区
        BufferedInputStream bis = new BufferedInputStream(in);
        //实际的读取
        byte[] bytes = new byte[101];
        int len = bis.read(bytes);
        String str = new String(bytes, 0, len);
        System.out.println(str);
        bis.close();
        in.close();
    }


    private static Scanner sc = new Scanner(System.in);

    //带缓冲的写入操作***
    private static void b() throws Exception {
        File file = new File("F:\\yongpangaofeng\\d.txt");
        OutputStream out = new FileOutputStream(file);
        //创建缓冲区
        BufferedOutputStream bos=new BufferedOutputStream(out);
        //所有的写入操作都交给bos执行
        while (true){
            System.out.println("请输入:");
            String str=sc.next();
            if("exit".equals(str)){
                break;
            }
            byte[] bytes=(str+"\n").getBytes();
            //写入操作，但是这里没有写入硬盘，而是写入了缓冲区中
            bos.write(bytes);
        }
        bos.close();
        //才会真正写到硬盘中
        out.close();
    }

    public static void main(String[] args) throws Exception {
//        a();
        b();
    }
}
